﻿
Imports remotecall
Imports centrosphere.gravitation_rc_pac

Namespace gravitation_rc_pac
    Public Class naming
        Public Const ijob As String = "ijob"
        Public Const result As String = "result"
        Public Const jobId As String = "jobId"
        Public Const queueName As String = "queueName"
    End Class
End Namespace

Public Class gravitation_rc_pac_star
    Inherits gravitation_star(Of rc_pac_serve, envelope, envelope)

    Protected Overrides Function create_output(ByVal field_count As Int32) As envelope
        Return New envelope()
    End Function

    Protected Overrides Function create_server(ByVal port As Int32, ByVal connectionCount As Int64) As rc_pac_serve
        Return New rc_pac_serve(port)
    End Function

    Protected Overrides Sub inject_output_ijob(ByVal output As envelope, ByVal job As ijob)
        assert(output.add(naming.ijob, job))
    End Sub

    Protected Overrides Sub inject_output_jobid(ByVal output As envelope, ByVal index As fromBytes)
        assert(output.add(naming.jobId, index))
    End Sub

    Protected Overrides Sub inject_output_result(ByVal output As envelope, ByVal result As fromBytes)
        assert(output.add(naming.result, result))
    End Sub

    Protected Overrides Function parse_input_ijob(ByVal input As envelope, ByRef o As ijob) As Boolean
        Return input.get(Of ijob)(naming.ijob, o)
    End Function

    Protected Overrides Function parse_input_jobId(ByVal input As envelope, ByRef o As bytesInt64) As Boolean
        Return input.get(Of bytesInt64)(naming.jobId, o)
    End Function

    Protected Overrides Function parse_input_queuename(ByVal input As envelope, ByRef o As bytesString) As Boolean
        Return input.get(Of bytesString)(naming.queueName, o)
    End Function

    Protected Overrides Function parse_input_valid(ByVal input As envelope) As Boolean
        Return Not input Is Nothing
    End Function

    Protected Overrides Sub register(ByVal server As rc_pac_serve,
                                     ByVal name As String,
                                     ByVal callback As _do(Of envelope, pointer(Of envelope), eventComb))
        assert(server.register(name, callback))
    End Sub

    Protected Overrides Function start_server(ByVal server As rc_pac_serve,
                                              ByVal port As Int32,
                                              ByVal connectionCount As Int64) As Boolean
        server.maxConnectionCount() = connectionCount
        Return True
    End Function

    Protected Overrides Sub stop_server(ByVal server As rc_pac_serve)
    End Sub
End Class

Public Class gravitation_rc_pac_planet
    Inherits gravitation_planet(Of rc_pac_client_pool, response)

    Protected Overrides Function create_client(ByVal host As String, ByVal port As Int32) As rc_pac_client_pool
        Return New rc_pac_client_pool(host, port)
    End Function

    Protected Overrides Function invoke(ByVal client As rc_pac_client_pool,
                                        ByVal name As String,
                                        ByRef ec As eventComb,
                                        ByVal rtn As pointer(Of response),
                                        ByVal ParamArray paras() As toBytes) As Boolean
        If paras.Length() = 1 Then
            Dim send As envelope = Nothing
            send = New envelope()
            assert(send.add(naming.queueName, paras(0)))
            Return client.invoke(New request(name, send), rtn, ec)
        ElseIf paras.Length() = 3 Then
            Dim send As envelope = Nothing
            send = New envelope()
            assert(send.add(naming.queueName, paras(0)))
            assert(send.add(naming.jobId, paras(1)))
            assert(send.add(naming.ijob, paras(2)))
            Return client.invoke(New request(name, send), rtn, ec)
        Else
            Return assert(False)
        End If
    End Function

    Protected Overrides Function parse_return_job(ByVal r As response,
                                                  ByRef o As ijob) As Boolean
        Return r.argument.get(naming.ijob, o)
    End Function

    Protected Overrides Function parse_return_sourceIndex(ByVal r As response,
                                                          ByRef o As bytesInt64) As Boolean
        Return r.argument.get(naming.jobId, o)
    End Function

    Protected Overrides Function parse_return_success(ByVal r As response,
                                                      ByRef o As bytesBoolean) As Boolean
        Return r.argument.get(naming.result, o)
    End Function

    Protected Overrides Function parse_return_valid(ByVal r As response) As Boolean
        Return Not r Is Nothing AndAlso Not r.argument Is Nothing
    End Function
End Class
